package com.reins.bookstore.utils.pageutils;

import lombok.Data;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import org.json.JSONObject;

import javax.crypto.MacSpi;
@Data
public class Page {
    private int status = 1; // 1为正常
    private int now_page;
    private int total_page;
    private int capacity = 12;
    private JSONArray data;

    Page(int now_page,JSONArray data){
        this.now_page = now_page;
        int start = (now_page-1)*capacity;
        int len = data.size();
        total_page = (len+capacity-1)/capacity;
        if (start >= len) {
            data = null;
            status = 0; //0表示没有数据了
            return;
        }
        this.data = new JSONArray();
        int up_size = 0;
        // 选择小的那个作为上界
        if (start + capacity <= len)
            up_size = start +capacity;
        else
            up_size = len;
        for (int i = start; i < up_size; i++){
            this.data.add(data.get(i)); //一个一个加入
        }
    }
    Page(int now_page,int capacity, JSONArray data){
        this.capacity = capacity;
        this.now_page = now_page;
        int start = (now_page-1)*capacity;
        int len = data.size();
        total_page = len/capacity +1;
        if (start >= len) {
            data = null;
            status = 0; //0表示没有数据了
        }
        int up_size = 0;
        // 选择小的那个作为上界
        if (start + capacity <= len)
            up_size = start +capacity;
        else
            up_size = len;
        for (int i = start; i < len; i++){
            this.data.add(data.get(i)); //一个一个加入
        }
    }
}
